package com.mybatisplus.mp.controller;

import com.mybatisplus.mp.pojo.Group;
import com.mybatisplus.mp.pojo.MyPojoTest;
import com.mybatisplus.mp.pojo.User;
import com.mybatisplus.mp.pojo.ValidPojo;
import com.mybatisplus.mp.service.VersionLockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.Date;

@RestController
public class VersionLockDemoController {
    @Autowired
    private VersionLockService versionLockService;

    @PostMapping(value = "/versionLock")
    public void versionLock(@RequestBody User user) {
        versionLockService.versionLock(user);
    }

    //case1 含有分组，但是@Valid加分组：只会校验分组里的
    @PostMapping(value = "/testValid")
    public void testListValid(@Validated(Group.class) @RequestBody ValidPojo validPojo) {
        System.out.println(validPojo);
    }

    //case2 含有分组，@Validated不加分组：只会检验未加分组的字段
    @PostMapping(value = "/testValid2")
    public void testListValid2(@Validated @RequestBody ValidPojo validPojo) {
        System.out.println(validPojo);
    }

    //case3 含有分组，@Validated不加分组：只会检验未加分组的字段
    @PostMapping(value = "/testValid3")
    public void testListValid3() {
        @NotNull
        @Valid String s = null;
        System.out.println("hello");
    }

    @PostMapping(value = "/test4")
    public void test4(@Validated(Group.class) @RequestBody ValidPojo validPojo) {

    }
    @PostMapping(value = "/test5")
    public void test5(@Validated @RequestBody ValidPojo validPojo) {

    }

    //测试 时间格式化
    @PostMapping(value = "/test6")
    public MyPojoTest test6() {
        MyPojoTest build = MyPojoTest.builder().value(new Date()).build();
        return build;
    }

    //测试 aop的返回值
    @PostMapping(value = "/test7")
    public User test7() {
        User controller = User.builder().name("controller").build();
        versionLockService.around(controller);
        return controller;
    }


}
